package com.jeeplus.invoice.api;

import com.jeeplus.invoice.customer.service.YxfpCustomerService;
import com.jeeplus.invoice.customer.service.mapstruct.YxfpCustomerWrapper;
import com.jeeplus.invoice.merchant.service.YxfpMerchantService;
import com.jeeplus.invoice.merchant.service.dto.YxfpMerchantDTO;
import com.jeeplus.invoice.merchant.service.mapstruct.YxfpMerchantWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "越香发票-商户管理API")
@RestController
@RequestMapping(value = "/invoice/api/merchant")
public class MerchantAPI {

    @Autowired
    private YxfpMerchantService yxfpMerchantService;

    @Autowired
    private YxfpMerchantWrapper yxfpMerchantWrapper;

    /**
     * 根据Id获取商户数据
     */
    @ApiOperation(value = "根据Id获取商户数据")
    @GetMapping("getMerchantById")
    public ResponseEntity<YxfpMerchantDTO> getMerchantById(String id) {
        return ResponseEntity.ok(yxfpMerchantService.findById(id));
    }

    /**
     * 根据OpenId获取商户数据
     */
    @ApiOperation(value = "根据OpenId获取商户数据")
    @GetMapping("getMerchantByOpenId")
    public ResponseEntity<YxfpMerchantDTO> getMerchantByOpenId(String openId) {
        return ResponseEntity.ok(yxfpMerchantService.findByOpenId(openId));
    }

    /**
     * 保存商户数据
     */
    @ApiOperation(value = "保存商户数据")
    @PostMapping("saveMerchant")
    public  ResponseEntity <String> saveMerchant(@Valid @RequestBody YxfpMerchantDTO yxfpMerchantDTO) {
        //新增或编辑表单保存
        yxfpMerchantService.saveOrUpdate(yxfpMerchantWrapper.toEntity(yxfpMerchantDTO));
        return ResponseEntity.ok("保存商户数据成功");
    }

}
